/*
 * Class DropItem
 *
 * Version 1.0
 *
 * Thursday, August 21, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.player.*;
import palidino76.rs2.player.games.*;
import palidino76.rs2.player.followers.*;
import palidino76.rs2.*;
import palidino76.rs2.content.minigames.BountyHunter;

public class DropItem implements Packet {
    /**
     * Handles dropping items in your inventory.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        int junk = p.stream.readDWord();
        int itemSlot = p.stream.readUnsignedWordBigEndianA();
        int itemId = p.stream.readUnsignedWord();
		BountyHunter bH = new BountyHunter();
        if (itemSlot < 0 || itemSlot >= p.items.length || p.items[itemSlot] != itemId) {
            return;
        }
	Followers follow = new Followers(p);
	WarriorGuild wGuild = new WarriorGuild(p);
	boolean failedDropping = false;
		    if(p.inBounty) {
	  	//p.headIconPrayer = Engine.BountyHunter.setSkull(p);
		p.updateReq = p.appearanceUpdateReq = true;
	    }
        if (!Engine.items.isUntradable(itemId)) {
	    if (itemId == 702) {
		p.requestAnim(827, 0);
	        p.explodeType = 1;
		p.explodeDelay = 2;
	    }
	    if (itemId == 7581) {
		if (!p.summonedPet) {
			p.requestAnim(827, 0);
			Server.engine.newNPC(follow.getSummonNpc(itemId), p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, 0);
			follow.setSummonedNpc(true);
		}
	    }
	    switch (itemId) {

		case 12047:
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit wolf.");
				Server.engine.newNPC(6829, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

		case 12043:
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Dreadfowl.");
				Server.engine.newNPC(6825, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

		case 12025:
			if (p.getLevelForXP(23) <= 79)
				p.frames.sendMessage(p, "You need a summoning level of 80 to summon a Hydra.");
			if (p.getLevelForXP(23) <= 79)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Hydra");
				Server.engine.newNPC(6811, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

		case 12063:
			if (p.getLevelForXP(23) <= 25)
				p.frames.sendMessage(p, "You need a summoning level of 25 to summon a Spirit Kalphite.");
			if (p.getLevelForXP(23) <= 25)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit Kalphite.");
				Server.engine.newNPC(6994, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

 		case 12017:
			if (p.getLevelForXP(23) <= 82)
				p.frames.sendMessage(p, "You need a summoning level of 83 to summon a Spirit Dagannoth.");
			if (p.getLevelForXP(23) <= 82)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit Dagannoth.");
				Server.engine.newNPC(6804, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

 		case 12099:
			if (p.getLevelForXP(23) <= 42)
				p.frames.sendMessage(p, "You need a summoning level of 43 to summon a Spirit Saratrice.");
			if (p.getLevelForXP(23) <= 42)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit Saratrice.");
				Server.engine.newNPC(6879, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

 		case 12101:
			if (p.getLevelForXP(23) <= 42)
				p.frames.sendMessage(p, "You need a summoning level of 43 to summon a Spirit zamatrice.");
			if (p.getLevelForXP(23) <= 42)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit Zamatrice.");
				Server.engine.newNPC(6881, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

 		case 12105:
			if (p.getLevelForXP(23) <= 42)
				p.frames.sendMessage(p, "You need a summoning level of 43 to summon a Spirit Coraxatrice.");
			if (p.getLevelForXP(23) <= 42)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Spirit Coraxatrice.");
				Server.engine.newNPC(6885, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;

		case 12790:
			if (p.getLevelForXP(23) <= 98)
				p.frames.sendMessage(p, "You need a summoning level of 99 to summon a Steel Titan.");
			if (p.getLevelForXP(23) <= 98)
				return;
			if (!p.summonedFamiliar) {
				p.summonDrainDelay = 12;
				p.summonedFamiliar = true;
				p.frames.sendMessage(p, "You summon a Steel titan.");
				Server.engine.newNPC(7343, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
			} else {
				failedDropping = true;
				p.frames.sendMessage(p, "You cannot summon another familiar.");
			}
		break;
case 12469:
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your dragon on the ground.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6901, 663, 3);
Server.engine.newNPC(6901, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 6901;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
if(itemId == 12490)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your Squirrel.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6920, 663, 3);
Server.engine.newNPC(6920, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 6920;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
if(itemId == 12508)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your Zamorak Hawk.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6954, 663, 3);
Server.engine.newNPC(6954, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);;
//p.FamiliarType = 6954;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
else if(itemId == 12471)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your dragon on the ground.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6903, 663, 3);
Server.engine.newNPC(6903, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 6903;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
else if(itemId == 12473)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your dragon on the ground.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6905, 663, 3);
Server.engine.newNPC(6905, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 6905;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
else if(itemId == 12476)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your !KING BLACK DRAGON! on the ground.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 50, 663, 3);
Server.engine.newNPC(50, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 50;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}
else if(itemId == 12475)
{ 
if(p.FamiliarID == 0)
{
if(p.skillLvl[23] != 99)
{
p.frames.sendMessage(p, "You need 99 summoning to drop this pet.");
}
else
{
p.requestAnim(827, 0);
p.frames.sendMessage(p, "You drop your dragon on the ground.");
p.frames.setTab(p, 80, 663);
p.frames.animateInterfaceId(p, 9850, 663, 3);
p.frames.setNPCId(p, 6907, 663, 3);
Server.engine.newNPC(6907, p.absX-1, p.absY, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
//p.FamiliarType = 6907;
  Engine.playerItems.deleteItem(p, itemId, itemSlot,p.itemsN[itemSlot]);
}
}
}

		case 1163:
		case 1127:
		case 1079:
			if (p.absX == 2857 && p.absY == 3537 && p.heightLevel == 0 || p.absX == 2851 && p.absY == 3537 && p.heightLevel == 0) {
				p.warriorArmour = "Rune";
				if (wGuild.hasCorrectEquipment()) {
					p.requestAnim(827, 0);
					wGuild.removeWarriorEquipment();
					Server.engine.newNPC(4284, p.absX, p.absY - 1, p.heightLevel, 0, 0, 0, 0, false, p.playerId);
				} else {
					p.frames.sendMessage(p, "You need a full Rune armour to go further with this process.");
					failedDropping = true;
				}
				p.warriorArmour = "";
			}
		break;

		case 6570:
		case 10566:
			p.frames.sendMessage(p, "The fire cape vanished into the ground.");
		break;
	    }
	    if (itemId != 702 && !wGuild.animatedRoom() && itemId != 12047 && itemId != 12790 && itemId != 12063 && itemId != 12025 && !failedDropping && itemId != 6570 && itemId != 10566)
            	Engine.items.createGroundItem(itemId, p.itemsN[itemSlot], p.absX, p.absY, p.heightLevel, p.username);
	    if (!failedDropping)
            	Engine.playerItems.deleteItem(p, itemId, itemSlot, p.itemsN[itemSlot]);
	    failedDropping = false;
        } else if (Engine.items.isUntradable(itemId)) {
            for (int i = 0; i < p.items.length; i++) {
		p.destroyItem[i] = itemId;
		p.destroyItemAmt[i] = 1;
		p.destroyItemInt = itemId;
		p.frames.showChatboxInterface(p, 94);
		p.frames.setString(p, "Clicking yes will permanently delete this item.", 94, 11);
		p.frames.setString(p, "", 94, 10);
		p.frames.setString(p, "If you want to keep this item click no.", 94, 12);
		p.frames.setString(p, ""+ Engine.items.getItemName(itemId) +"", 94, 13);
		p.frames.setItems(p, 94, 0, 1, p.destroyItem, p.destroyItemAmt);
	    }
	}
    }

    boolean nearEntranceKQ(Player player) {
	return player.absX >= 3508 && player.absX <= 3511 && player.absY >= 9496 && player.absY <= 9499 && player.heightLevel == 2;
    }
}
